package com.wz.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.wz.activity.ChooseMesgActivity;
import com.wz.activity.R;
import com.wz.bean.Festival;
import com.wz.bean.FestivalData;

/**
 * 显示节日列表
 *
 */
public class FestivalCategoryFragment extends Fragment {

	public static final String FESTIVAL_ID = "festival_id";
	private GridView gridView;
	private ArrayAdapter<Festival> madapter;

	private LayoutInflater minInflater;

	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

		return inflater.inflate(R.layout.frag_festival_category, container, false);
	}

	//先与onCreateView 执行
	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		minInflater = LayoutInflater.from(this.getActivity());
		gridView = (GridView) view.findViewById(R.id.wz_gridview_festival_category);

		// 使用匿名内部类的方式,从写 ArrayAdapter
		gridView.setAdapter(madapter = new ArrayAdapter<Festival>(this.getActivity(), -1, FestivalData.getInstance().getFestivals()) {

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				if (convertView == null) {
					convertView = minInflater.inflate(R.layout.item_festival, parent, false);
				}
				TextView tv = (TextView) convertView.findViewById(R.id.wz_tv_festival_name);
				tv.setText(this.getItem(position).getName());
				return convertView;
			}
		});
		
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				
				Intent intent = new Intent(FestivalCategoryFragment.this.getContext(), ChooseMesgActivity.class);
				intent.putExtra(FESTIVAL_ID, madapter.getItem(position).getId());
				startActivity(intent);
				
			}
		});

	}

}
